/*
 * Copyright (c) 2016. For Intelligent Group.
 */
package com.intelligent.ispc.client.core.controller;

import com.intelligent.ispc.client.event.ThrowExceptionEvent;
import com.intelligent.ispc.client.utils.EventBusUtils;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.constant.AppConstant;
import com.intelligent.ispc.core.dto.FileDataDto;
import com.intelligent.ispc.core.dto.KeyValueDto;
import com.intelligent.ispc.core.service.FileDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Peter on 2016/7/12.
 */
@Component
public class GlobalSearchController {
    private final Logger logger = LoggerFactory.getLogger(GlobalSearchController.class);

    @Autowired
    private FileDataService fileDataService;

    public void importFile(List<KeyValueDto> keyValueDtos) {
        logger.debug("SpcController importFile keyValueDtos is:" + keyValueDtos);
        try {
            fileDataService.importSpcData(keyValueDtos);
        } catch (ApplicationException e) {
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        }
    }

    public void checkFile(List<FileDataDto> fileDataDtos) {
        try {
            fileDataService.updateSpcFileDatas(fileDataDtos);
        } catch (ApplicationException e) {
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        }
    }

    public List<String> validateFiles(List<KeyValueDto> files) {
        List<String> re = new ArrayList<>();
        try {
            List<String> names = new ArrayList<>();
            for (KeyValueDto dto : files) {
                names.add(dto.getKey());
            }
            List<FileDataDto> results = fileDataService.validateFileName(names, AppConstant.FILE_IMPORT_TYPE_SPC);
            for (FileDataDto dto : results) {
                re.add(dto.getName());
            }
        } catch (ApplicationException e) {
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        }

        return re;
    }

    public List<FileDataDto> findFiles() {
        List<FileDataDto> fileDataDtos = null;
        try {
            fileDataDtos = fileDataService.findFileData(AppConstant.FILE_IMPORT_TYPE_SPC);
        } catch (ApplicationException e) {
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        }
        logger.debug("SpcController findFiles is :" + fileDataDtos);

        return fileDataDtos;
    }

    public void deleteFiles(List<FileDataDto> fileDataDtos) {
        try {
            for (int i = 0; i < fileDataDtos.size(); i++) {
                String name = fileDataDtos.get(i).getName();
                //Long l = fileDataDtos.get(i).getId();
                fileDataService.deleteFileDataByName(name, AppConstant.FILE_IMPORT_TYPE_SPC);
            }
        } catch (ApplicationException e) {
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        }
        logger.debug("SpcController deleteCsvName");
    }

    public List<FileDataDto> reloadFile() {
        try {
            logger.debug("SpcController reloadFile");
            return fileDataService.reloadData(AppConstant.FILE_IMPORT_TYPE_SPC);
        } catch (ApplicationException e) {
            EventBusUtils.post(new ThrowExceptionEvent(e.getFullMessage()));
        }
        return null;

    }
}
